home *** CD-ROM | disk | FTP | other *** search
/ Power Bytes: Money & Finance / PowerBytes Money and Finance CD-ROM 01 / PowerBytes Money and Finance CD-ROM 01.iso / HyperCard Files / MacMathPascal / card_5618.txt < prev    next >
Encoding:
Text File  |  1988-09-05  |  3.7 KB  |  143 lines

  1. -- card: 5618 from stack: in
  2. -- bmap block id: 0
  3. -- flags: 0000
  4. -- background id: 2607
  5. -- name: Area.Pas
  6.  
  7.  
  8. -- part contents for background part 22
  9. ----- text -----
  10. Area.Pas
  11.  
  12. -- part contents for background part 23
  13. ----- text -----
  14. Area.Pas
  15.  
  16. -- part contents for background part 17
  17. ----- text -----
  18. AREA[function]
  19.  
  20.  
  21. PURPOSE 
  22.  
  23. AREA will calculate the integral of a function defined by the user supplied function F.  AREA uses various Newton-Cotes formulas to calculate the integral by quadrature.  The particular formula used is determined by the number of integration points.  If the number of points minus one is divisible by six a formula will be used with an error on the order of dx9.  If this does not hold, then the integral will be divided into two sections. One section will contain 6N+1 points and the other will contain the rest of the function.  If one point remains the trapezoid rule will be used.  If two points remain Simpson's one third rule is applied.  Simpson's three-eighths rule is next.   It is desirable for speed and accuracy to use a number of points such that the auxiliary functions need not be used.  If possible, use (6N +1) points.
  24.  
  25.  
  26. TYPE REQUIREMENTS
  27.  
  28. TYPE
  29. FLOAT = REAL or DOUBLE or EXTENDED;
  30. FIXXED = INTEGER or LONGINT;
  31.  
  32.  
  33. CALLING PROCEDURE
  34.  
  35. var
  36. XSTART:FLOAT;{Starting point for the integration}
  37. XFINISH:FLOAT;{Ending point for the integration}
  38. POINTS:FIXXED;{Number of integration points}
  39.  
  40. Define the function F of type FLOAT with input XIN also of type FLOAT.
  41. Define XSTART, XFINISH, and POINTS.  Then call
  42.  
  43. INTEGRAL_OF_FUNCTION:=AREA(XSTART,XFINISH,POINTS);
  44.  
  45.  
  46.  
  47.  
  48.  
  49. EXAMPLE
  50. The example calculates Γê½sin(x)dx for 0 to ΓêÅ/2 using 3 to 100 points.  This value along with the number of integration points is printed.
  51.  
  52.  
  53. REFERENCES
  54.  
  55. James, M. L., Smith, G. M., Wolford, J. C.:  Applied Numerical Methods for Digital Computation With Fortran and CSMP, Harper and Row, New York, 1977.
  56.  
  57.  
  58.  
  59. -- part contents for background part 26
  60. ----- text -----
  61. 15
  62.  
  63. -- part contents for background part 6
  64. ----- text -----
  65. PROGRAM TEST_AREA;
  66. TYPE
  67. FLOAT = REAL;
  68. FIXXED = INTEGER;
  69. VAR
  70. XMIN, XMAX, YOUT : FLOAT;
  71. N : FIXXED;
  72. PROCEDURE see;
  73. VAR
  74. R : Rect;
  75. BEGIN
  76. HideAll;
  77. SetRect(R, 0, 35, 550, 330);
  78. SettextRect(R);
  79. Showtext;
  80. END;
  81.  
  82. FUNCTION F (X : FLOAT) : FLOAT;
  83. BEGIN
  84. F := SIN(X);
  85. END;
  86. FUNCTION AREA (XSTART, XFINISH : FLOAT;
  87. POINTS : FIXXED) : FLOAT;
  88. VAR
  89. I, N, REMAIN : FIXXED;
  90. SUM, LEFT, X, H : FLOAT;
  91. BEGIN
  92. N := (POINTS - 1) DIV 6;
  93. REMAIN := POINTS - 1 - N * 6;
  94. SUM := 0.0;
  95. LEFT := 0.0;
  96. H := (XFINISH - XSTART) / (POINTS - 1);
  97. {WRITELN(H : 10 : 5);}
  98. IF (N > 0) THEN
  99. FOR I := 1 TO N DO
  100. BEGIN
  101. X := XSTART + (I - 1) * H * 6;
  102. SUM := SUM + (41 * F(X) + 216 * F(X + H) + 27 * F(X + 2 * H));
  103. SUM := SUM + (272 * F(X + 3 * H) + 27 * F(X + 4 * H));
  104. SUM := SUM + (216 * F(X + 5 * H) + 41 * F(X + 6 * H));
  105. END;
  106. SUM := (SUM * H / 140.0);
  107. IF (REMAIN > 0) THEN
  108. BEGIN
  109. X := XSTART + N * H * 6;
  110. IF (REMAIN = 1) THEN
  111. LEFT := (F(X) + F(XFINISH)) / 2.0;
  112. IF (REMAIN = 2) THEN
  113. LEFT := (F(X) + 4.0 * F(X + H) + F(XFINISH)) / 3.0;
  114. IF (REMAIN = 3) THEN
  115. LEFT := 3.0 * (F(X) + 3.0 * F(X + H) + 3.0 * F(X + 2 * H) + F(XFINISH)) / 8.0;
  116. IF (REMAIN = 4) THEN
  117. BEGIN
  118. LEFT := (7 * F(X) + 32 * F(X + H));
  119. LEFT := (12 * F(X + 2 * H) + 32 * F(X + 3 * H) + 7 * F(XFINISH)) + LEFT;
  120. LEFT := LEFT * (2.0 / 45.0);
  121. END;
  122. IF (REMAIN = 5) THEN
  123. BEGIN
  124. LEFT := (19 * F(X) + 75 * F(X + H));
  125. LEFT := (50 * F(X + 2 * H) + 50 * F(X + 3 * H)) + LEFT;
  126. LEFT := (75 * F(X + 4 * H) + 19 * F(XFINISH)) + LEFT;
  127. LEFT := LEFT * (5.0 / 288.0);
  128. END;
  129. LEFT := LEFT * H;
  130. END;
  131. AREA := SUM + LEFT;
  132. END;
  133. BEGIN
  134. see;
  135. XMIN := 0.0;
  136. XMAX := ARCTAN(1.0) * 2.0;
  137. WRITELN('  INTEGRATION OF SIN(X) FOR X = 0 TO PI');
  138. FOR N := 3 TO 100 DO
  139. BEGIN
  140. YOUT := AREA(XMIN, XMAX, N);
  141. WRITELN(' NUMBER OF POINTS ', N : 5, ' VALUE OF INTEGRATION ', YOUT : 20 : 10);
  142. END;
  143. END.